const {createVideo, getVideoInfo, updateVideoInfo} = require('../service/video.service');

const {databaseError} = require('../constant/err.type');

const {createLog} = require('../service/log.service');


class VideoController {

    async addVideo(ctx, next) {
        let {video_name, video_desc, video_url, banner_url, video_type, video_source} = ctx.request.body;
        // 操作数据库
        try {
            let res = await createVideo(video_name, video_desc, video_url, banner_url, video_type, video_source);
            if (res) {
                // 记录日志
                await createLog(ctx, `添加视频成功：${video_name}`);
                ctx.body = {
                    code: 1,
                    message: '视频添加成功！',
                    result: {
                        id: res.id,
                        video_name: res.video_name
                    }
                };
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx)
        }
    }

    async updateVideo(ctx, next) {
        // 获取数据
        let {id, video_name, video_desc, video_url, banner_url, video_type, video_source} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateVideoInfo({
                id,
                video_name,
                video_desc,
                video_url,
                banner_url,
                video_type,
                video_source
            });
            if (res) {
                // 记录日志
                await createLog(ctx, `修改视频成功，视频ID：${video_name}`);
                ctx.body = {
                    code: 1,
                    message: '视频修改成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }

    async queryVideo(ctx, next) {
        // 可供使用的筛选条件
        let {id, video_name, video_desc, video_type, video_source, flag, pageInfo} = ctx.request.query;

        try {
            let res = await getVideoInfo({id, video_name, video_desc, video_type, video_source, flag, pageInfo});
            ctx.body = {
                code: 1,
                message: '视频查询成功！',
                data: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async queryVideoDetail(ctx, next) {
        let {id} = ctx.request.query;
        try {
            let res = await getVideoInfo({id});
            ctx.body = {
                code: 1,
                message: '视频详情查询成功！',
                result: res
            }

        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }

    }

    async deleteVideo(ctx, next) {
        // 获取数据
        let {id, flag} = ctx.request.body;
        // 操作数据库
        try {
            let res = await updateVideoInfo({id, flag});
            if (res) {
                // 记录日志
                await createLog(ctx, `删除视频成功，视频ID：${id}`);
                ctx.body = {
                    code: 1,
                    message: '视频删除成功！',
                    result: {}
                }
            } else {
                ctx.app.emit('error', databaseError, ctx);
            }
        } catch (e) {
            ctx.app.emit('error', databaseError, ctx);
        }
    }
}

module.exports = new VideoController();